﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VehicleRecordSystem.view
{
    /// <summary>
    /// Interaction logic for RepairPage.xaml
    /// </summary>
    public partial class RepairPage : Page
    {
        private static RepairPage instanse;
        private string errorBackgroundColor = "#EBA4B0";

        public static RepairPage Instanse
        {
            get
            {
                if (instanse == null)
                {
                    instanse = new RepairPage();
                }

                return instanse;
            }
        }

        public RepairPage()
        {
            InitializeComponent();
        }

        private void btnRepairPageSave_Click(object sender, RoutedEventArgs e)
        {
            if (validate())
            {
                String garage_name = txtGarageName.Text;
                DateTime? repair_date = datePickerRepairDate.SelectedDate;
                String repair_type = comboboxRepairType.SelectedValue.ToString();
                String description = txtDescription.Text;
                BitmapImage invoice_image = new BitmapImage(new Uri(txtInvoiceImagePath.Text));
            }
        }

        private Boolean validate()
        {
            var bc = new BrushConverter();
            Boolean ok=true;

            if (string.IsNullOrEmpty(txtGarageName.Text))
            {
                ok = false;
                txtGarageName.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(datePickerRepairDate.SelectedDate.ToString()))
            {
                ok = false;
                datePickerRepairDate.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(comboboxRepairType.Text))
            {
                ok = false;
                comboboxRepairType.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(txtDescription.Text))
            {
                ok = false;
                txtDescription.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (!validUri(txtInvoiceImagePath.Text))
            {
                ok = false;
                txtInvoiceImagePath.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }

            return ok;
        }

        private Boolean validUri(String path)
        {
            Boolean ok = true;
            try
            {
                new Uri(path);
            }
            catch
            {
                ok = false;
            }
            return ok;
        }

        private void changeBackgroundColorToNormal(object sender, RoutedEventArgs e)
        {
            var bc = new BrushConverter();

            if (sender is TextBox)
            {
                TextBox txt = (TextBox)sender;
                txt.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
            if (sender is DatePicker)
            {
                DatePicker dp = (DatePicker)sender;
                dp.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
            if (sender is ComboBox)
            {
                ComboBox dp = (ComboBox)sender;
                dp.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
        }

        private void btnInvoiceImageBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                openFileDialog.ShowDialog();
                openFileDialog.Title = "Set Insurance Card Image";
                openFileDialog.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "Portable Network Graphic (*.png)|*.png";
                openFileDialog.DefaultExt = ".jpeg";
                txtInvoiceImagePath.Text = openFileDialog.FileName;

               /* ImageSource imageSource = new BitmapImage(new Uri(image_textBox.Text));
                imageRegistrationCertificate.Source = imageSource;
                */
            }
            catch (Exception)
            {
                MessageBox.Show("Image Uploading Error", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }


    }
}
